package com.sxt.business.controller;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sxt.business.domain.Customer;
import com.sxt.business.service.ICustomerService;
import com.sxt.business.vo.CustomerVO;
import com.sxt.system.controller.DeptController;
import com.sxt.system.utils.DataGridView;
import com.sxt.system.utils.ResultObj;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 俊哥
 * @since 2019-11-15
 */
@RestController
@RequestMapping("customer")
public class CustomerController {

	@Autowired
	private ICustomerService customerService;
	
	private Log log = LogFactory.getLog(DeptController.class);
	
	/**
	 * 	全查询
	 * @param customerVO
	 * @return
	 */
	@RequestMapping("loadAllCustomer")
	public DataGridView loadAllCustomer(CustomerVO customerVO) {
		return this.customerService.loadAllCustomer(customerVO);
	}
	
	/**
	 * 	添加
	 */
	@RequestMapping("addCustomer")
	public ResultObj addCustomer(Customer customer ,HttpSession session) {
		try {
			customerService.addCustomer(customer);
			return ResultObj.ADD_SUCCESS;
		} catch (Exception e) {
			log.error("添加失败");
			return ResultObj.ADD_ERROR;
		}
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("updateCustomer")
	public ResultObj updateCustomer(Customer customer) {
		try {
			customerService.updateCustomer(customer);
			return ResultObj.UPDATE_SUCCESS;
		} catch (Exception e) {
			log.error("修改失败");
			return ResultObj.UPDATE_ERROR;
		}
	}
	
	/**
	 * 	删除
	 */
	@RequestMapping("deleteCustomer")
	public ResultObj deleteCustomer(Integer id) {
		try {
			customerService.removeById(id);
			return ResultObj.DELETE_SUCCESS;
		} catch (Exception e) {
			log.error("删除失败");
			return ResultObj.DELETE_ERROR;
		}
	}
	
	/**
	 * 批量删除
	 */
	@RequestMapping("batchDeleteCustomer")
	public ResultObj batchDeleteCustomer(Integer [] ids) {
		try {
			if(ids==null||ids.length==0) {
				log.error("参数不能为空");
				return ResultObj.DELETE_ERROR;
			}
			Collection<Serializable> idList = new ArrayList<Serializable>();
			for (Integer  integer  : ids) {
				idList.add(integer);
			}
			this.customerService.removeByIds(idList);
			return ResultObj.DELETE_SUCCESS;
		} catch (Exception e) {
			log.error("删除失败");
			return ResultObj.DELETE_ERROR;
		}
	}
	
	/**
	 * 全查询 不分页
	 */
	@RequestMapping("loadAllAvailableCustomer")
	public DataGridView loadAllAvailableCustomer() {
		return this.customerService.loadAllAvailableCustomer();
	}
}

